package com.help10000.rms.ui.activitys

import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import com.amir.common.beans.OrderDetailData
import com.amir.common.beans.SectionData
import com.amir.common.beans.SectionItemData
import com.amir.common.weight.DuplicateClickProxy
import com.help10000.rms.R
import com.help10000.rms.common.PlayerState
import com.help10000.rms.common.VoicePlayer
import com.help10000.rms.mvp.presenter.OrderDetailsPresenter
import com.help10000.rms.mvp.view.OrderDetailsView
import com.help10000.rms.ui.adapter.OrderDetailAdapter
import com.help10000.rms.ui.base.BaseActivity
import com.lxj.xpopup.XPopup
import kotlinx.android.synthetic.main.activity_navigation.*
import kotlinx.android.synthetic.main.activity_order_details.*

/**
 * 订单详情
 */
class OrderDetailActivity : BaseActivity<OrderDetailsPresenter, OrderDetailsView>(),
    OrderDetailsView {
    var sections = mutableListOf<SectionItemData>()
    var sectionIndex = mutableListOf<Int>()
    var sectionTitles = mutableListOf<String>()
    var repairId: String? = null
    var data: OrderDetailData? = null
    override fun getlayoutId(): Int {
        return R.layout.activity_order_details
    }

    override fun initView() {

    }

    override fun initData(savedInstanceState: Bundle?) {
        if (savedInstanceState != null)
            repairId = savedInstanceState.getString("repairId")
        else
            repairId = intent.getStringExtra("repairId")
        if (repairId.isNullOrEmpty()) {
            finish()
        } else
            mPresenter.getOrderDetail(repairId!!, activity = this)
    }

    override fun initListener() {
        orderSectionMenu.setOnClickListener(DuplicateClickProxy {
            XPopup.Builder(this)
                .asBottomList(
                    "快捷导航",
                    sectionTitles.toTypedArray()
                ) { position, text ->
                    var mLayoutManager = itemsRecyclerView.layoutManager as LinearLayoutManager
                    mLayoutManager.scrollToPositionWithOffset(sectionIndex[position], 0)
                }.show()
        })
    }


    override fun createPresenter(): OrderDetailsPresenter {
        return OrderDetailsPresenter(view = this)
    }


    override fun descSuccess(data: OrderDetailData) {
        this.data = data
        var dataList = mutableListOf<SectionData>()
        var pos = 1
        for (index in data.Details) {
            var sectionItemData = SectionItemData(index.TabName, pos)
            sections.add(sectionItemData)
            if (!index.TabName.isEmpty()) {
                sectionTitles.add(index.TabName)
                sectionIndex.add(pos)
            }
            pos += index.Sections.size
            for (item in index.Sections) {
                dataList.add(item)
            }
        }
        itemsRecyclerView.apply {
            layoutManager = LinearLayoutManager(context)
            itemAnimator = null
            adapter = OrderDetailAdapter(
                context,
                dataList,
                data.Header,
                this@OrderDetailActivity
            )

        }
    }

    override fun playVoice(path: String) {
        var player = VoicePlayer()
        player.setStateListener(object : VoicePlayer.PlayStateListener {
            override fun onNotify(state: PlayerState) {
                when (state) {
                    PlayerState.PREPARING -> {
                        showLoading()
                    }
                    PlayerState.PLAYING -> {
                        dismissLoading()
                    }
                }
            }
        })
        player.play(path)
    }


}
